/*
 * Project it.battlehorse.rcp.tools
 * Created on Mar 31, 2006
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * A copy of the LGPL is available also at
 * http://www.gnu.org/copyleft/lesser.html
 * 
 */ 
package it.battlehorse.rcp.tools.dialogs;

import it.battlehorse.rcp.sl.annot.Injected;
import it.battlehorse.rcp.sl.annot.Serviceable;
import it.battlehorse.rcp.tools.actors.ActorRunnerEvent;
import it.battlehorse.rcp.tools.commons.EventType;
import it.battlehorse.rcp.tools.commons.Severity;
import it.battlehorse.rcp.tools.commons.actors.IValidationStatus;
import it.battlehorse.rcp.tools.log.IDetailHelper;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IconAndMessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.forms.events.ExpansionAdapter;
import org.eclipse.ui.forms.widgets.ExpandableComposite;

/**
 * Defines the dialog which shows to the user the results of a job invocation.
 * This dialog is instantiated by a {@code VisualActorRunnerListener }.
 *  
 * @author battlehorse
 * @since Mar 31, 2006
 */
@Serviceable
public class RunnerEventDialog extends IconAndMessageDialog {
	
	/* The event to be displayed */
	private ActorRunnerEvent event;
	
	private IDetailHelper helper;	

	/**
	 * Creates a new instance of the class
	 * 
	 * @param parentShell the parent shell
	 * @param event the event which is to be shown
	 */
	public RunnerEventDialog(Shell parentShell, ActorRunnerEvent event) {
		super(parentShell);	
		this.event = event;
		
		setMessage();
	}
	
	@Injected
	public void setDetailHelper(IDetailHelper helper) {
		this.helper = helper;
	}	
	
	/**
	 * Sets the message to be shown
	 */
	private void setMessage() {
		if (event.type == EventType.EXCEPTION_EVENT) {
			String exMessage = event.executionException.getMessage() == null ? 
					event.executionException.getClass().getName() :
					event.executionException.getMessage();
					
			this.message = "Error while executing the current job: \n" + exMessage;
		}
		else if (event.type == EventType.VALIDATION_EVENT) {
			this.message = event.validationStatus.getValidationMessage();			
		}			
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.IconAndMessageDialog#getImage()
	 */
	@Override
	protected Image getImage() {
		if (event.type == EventType.EXCEPTION_EVENT) {
			return super.getErrorImage();
		}
		else if (event.type == EventType.VALIDATION_EVENT) {
			IValidationStatus valStatus = event.validationStatus;
			if (valStatus.getSeverity().equals(Severity.WARNING)) {
				return super.getWarningImage();
			}
			else if (valStatus.getSeverity().equals(Severity.ERROR)) {
				return super.getErrorImage();
			}
			else {
				return super.getInfoImage();
			}
		}
		else
			return null;
	}
	
	@Override
	protected Control createDialogArea(Composite parent) {
		final Composite container = new Composite(parent, SWT.NONE);
		container.setLayout(new GridLayout(2,false));
		container.setLayoutData(new GridData(GridData.FILL_BOTH));
		
		createMessageArea(container);	
		
		Composite c = new Composite(container,SWT.NONE);	// cell empty filler
		GridData gd = new GridData();
		gd.widthHint = 1;
		gd.heightHint = 1;
		c.setLayoutData(gd);
		
		if (helper.haveDetails(event)) {
			// Details area
			ExpandableComposite expandable = new ExpandableComposite(container, SWT.NONE, ExpandableComposite.TWISTIE);
			expandable.setText("Show details");
			gd = new GridData(GridData.GRAB_HORIZONTAL
	                | GridData.HORIZONTAL_ALIGN_FILL
	                | GridData.VERTICAL_ALIGN_BEGINNING );		
			expandable.setLayoutData(gd);		
			
			Composite client = new Composite(expandable, SWT.NONE);
			client.setLayout(new GridLayout(1,false));
			expandable.setClient(client);
			expandable.addExpansionListener(new ExpansionAdapter() {
				@Override
				public void expansionStateChanged(org.eclipse.ui.forms.events.ExpansionEvent e) {
			        
			        Point newSize = getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT);
			        getShell().setSize(newSize);
			      
				}
			});
			
			helper.addDetailControl(client, event, 1, convertWidthInCharsToPixels(80), SWT.DEFAULT);
		}

		return container;
	}
	
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID,  IDialogConstants.OK_LABEL, true);
	}

}
